package com.tianhui.consignortwofei.mvp.ui.activity.audit.driver;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.other.RegionData;
import com.fgs.common.utils.IDCardUtils;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.IdCardInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.IdCardFrontStrategy;
import com.tianhui.consignortwofei.role.RoleController;
import com.xuexiang.xui.widget.picker.widget.OptionsPickerView;
import com.xuexiang.xui.widget.picker.widget.builder.OptionsPickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnOptionsSelectListener;

import butterknife.BindView;
import butterknife.OnClick;

public class FrontAuditActivity extends BaseDriverAuditActivity {


    @BindView(R.id.activity_front_audit_phoneInputItemView)
    InputItemView mPhoneInputItemView;
    @BindView(R.id.activity_front_audit_nameInputItemView)
    InputItemView mNameInputItemView;
    @BindView(R.id.activity_front_audit_idCardInputItemView)
    InputItemView mIdCardInputItemView;
    @BindView(R.id.activity_front_audit_regionClickItemView)
    ClickItemView mRegionClickItemView;
    @BindView(R.id.activity_front_audit_addressInputItemView)
    InputItemView mAddressInputItemView;
    @BindView(R.id.layout_audit_picture_editImageView)
    ImageView mEditImageView;

    private boolean mHasLoaded;
    private OptionsPickerView mProvincePickerView;
    private String mFrontImageId;
    private String mProvince;
    private String mCity;
    private String mArea;
    private String mCityCode;

    @Override
    public int getLayoutId() {
        return R.layout.activity_front_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }


    @Override
    public void init() {
        super.init();
        mHasLoaded = RegionData.getInstance().loadProvinceData(RegionData.TYPE_NORMAL);

    }

    @Override
    public void showDriverInfo(DriverInfo info) {
        mPhoneInputItemView.setContent(info.drivertel);
        mNameInputItemView.setContent(info.drivername);
        mIdCardInputItemView.setContent(info.driveridcard);
        mRegionClickItemView.setContent(info.driverprovince + " " + info.drivercity + " " + info.drivercounty);
        mAddressInputItemView.setContent(info.orderplacedetails);
        loadPicture(info.imgidcardtphoto);
        mFrontImageId = info.idcardtphoto;
        mProvince = info.driverprovince;
        mCity = info.drivercity;
        mArea = info.drivercounty;
        mCityCode = info.areacode;
        setInfoViewEnable(info.isaudit != 1);
    }

    private void setInfoViewEnable(boolean isEnable) {
        mNameInputItemView.setItemEnable(isEnable);
        mIdCardInputItemView.setItemEnable(isEnable);
        boolean isEditable = RoleController.getInstance().isCarrierEditable();

        if (isEnable) {
            mEditImageView.setVisibility(View.VISIBLE);
        } else {
            if (isEditable) {
                mEditImageView.setVisibility(View.VISIBLE);
            } else {
                mEditImageView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public DriverInfo getSaveInfo() {
        String name = mNameInputItemView.getContent();
        String idCardNumber = mIdCardInputItemView.getContent();
        String address = mAddressInputItemView.getContent();
        String regionCode = mCityCode;

        if (TextUtils.isEmpty(name)) {
            showToast("姓名不能为空");
            return null;
        }


        if (TextUtils.isEmpty(idCardNumber)) {
            showToast("身份证号不能为空");
            return null;
        }

        if (!IDCardUtils.validateCard(idCardNumber)) {
            showToast("请输入正确的身份证号");
            return null;
        }

        if (TextUtils.isEmpty(regionCode)) {
            showToast("请选择省市区");
            return null;
        }
        mDriverInfo.drivername = name;
        mDriverInfo.driveridcard = idCardNumber;
        mDriverInfo.orderplacedetails = address;
        mDriverInfo.idcardtphoto = mFrontImageId;

        mDriverInfo.driverprovince = mProvince;
        mDriverInfo.drivercity = mCity;
        mDriverInfo.drivercounty = mArea;
        mDriverInfo.areacode = mCityCode;

        return mDriverInfo;
    }

    @Override
    public void nextAudit() {
        DriverInfo info = getSaveInfo();
        if (info != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_DRIVER_INFO, info);
            bundle.putInt(KeyValue.KEY_OPERATE, getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(BackAuditActivity.class, bundle);
        }
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_front_audit_regionClickItemView)
    public void region() {
        hideKeyboard();
        if (!mHasLoaded) {
            showToast("数据加载中...");
            return;
        }
        if (mProvincePickerView == null) {

            mProvincePickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public boolean onOptionsSelect(View v, int options1, int options2, int options3) {
                    //返回的分别是三个级别的选中位置
//                    String province = RegionDataNew.getInstance().getProvince(options1);
//                    String city = RegionDataNew.getInstance().getCity(options1, options2);
//                    String region = RegionDataNew.getInstance().getArea(options1, options2, options3);
                    String province = RegionData.getInstance().getProvinceInfo(options1).name;
                    String city = RegionData.getInstance().getCityInfo(options1, options2).name;
                    String region = RegionData.getInstance().getAreaInfo(options1, options2, options3).name;

                    String cityCode = RegionData.getInstance().getCityInfo(options1, options2).code;
                    String regionCode = RegionData.getInstance().getAreaInfo(options1, options2, options3).code;

                    String tx = province + " " + city + " " + region;
                    mRegionClickItemView.setContent(tx);

                    mProvince = province;
                    mCity = city;
                    mArea = region;

                    if (!TextUtils.isEmpty(region)) {
                        mCityCode = regionCode;
                    } else if (TextUtils.isEmpty(region) && !TextUtils.isEmpty(city)) {
                        mCityCode = cityCode;
                    }


                    return false;
                }
            })

                    .setTitleText("城市选择")
                    .setDividerColor(Color.BLACK)
                    //切换选项时，还原到第一项
                    .isRestoreItem(true)
                    //设置选中项文字颜色
                    .setTextColorCenter(Color.BLACK)
                    .setContentTextSize(20)
                    .isDialog(false)
                    .setSelectOptions(RegionData.getInstance().getDefaultProvince(), RegionData.getInstance().getDefaultCity(), RegionData.getInstance().getDefaultArea())
                    .build();

            mProvincePickerView.setPicker(RegionData.getInstance().getProvinceItems(), RegionData.getInstance().getCityItems(), RegionData.getInstance().getAreaItems());//三级选择器
            mProvincePickerView.show();
        }
        mProvincePickerView.show();
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {
        mFrontImageId = pictureInfo.id;
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        IdCardFrontStrategy idCardFrontStrategy = (IdCardFrontStrategy) pictureStrategyFactory.createStrategy(IdCardFrontStrategy.class);
        pictureContext = new PictureContext(idCardFrontStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<IdCardInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<IdCardInfo> resultInfo) {
                mFrontImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processIdCardFrontResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processIdCardFrontResult(IdCardInfo resultInfo) {
        if (resultInfo == null) return;
        mNameInputItemView.setContent(resultInfo.name);
        mIdCardInputItemView.setContent(resultInfo.idNumber);
        mAddressInputItemView.setContent(resultInfo.address);
    }
}
